package tsing.javase.stream;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date 28/6/2023 下午4:06
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Person {
    private String name;

    private String id;

    private String gender;


    public static void main(String[] args) {

        List<Person> list  = new ArrayList<>();



        String gender = "男";
        Person person1 = Person.builder().gender(gender).name("张三").id("1").build();
        Person person2 = Person.builder().gender(gender).name("张三").id("2").build();
        Person person3 = Person.builder().gender("女").name("李四").id("3").build();
        Person person4 = Person.builder().gender("女").name("王五").id("4").build();
        list.add(person1);
        list.add(person2);
        list.add(person3);
        list.add(person4);

      /*  // 进行两轮分组
        Map<String, Map<String, List<Person>>> collect = list.stream()
                .collect(Collectors.groupingBy(
                        Person::getGender,
                        Collectors.groupingBy(Person::getName)
                ));

        System.out.println(JSON.toJSONString(collect));
        System.out.println(JSON.toJSONString(collect.keySet()));*/


        List<Person> people = new ArrayList<>();
        Person person5 = Person.builder().gender(gender).name("张三").id("3").build();
        Person person6 = Person.builder().gender(gender).name("张三").id("5").build();
        people.add(person5);
        people.add(person6);

        List<Person> collect = list.stream().filter(p1 -> people.stream().anyMatch(p2 -> p2.getId().equals(p1.getId()))).collect(Collectors.toList());

        collect.forEach(e-> e.setGender("人妖"));

        System.out.println(JSON.toJSONString(list));
        System.out.println(JSON.toJSONString(people));


    }
}
